using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.RecipeFramework;
using VSSDKAssist.Assets.Helpers;
using EnvDTE;
using VSSDKAssist.Assets.Actions.Base;
using System.Globalization;

namespace VSSDKAssist.Recipes.ConfigureVSPackage
{
	/// <summary>
	/// Action that adds/update the AutoLoad attribute
	/// </summary>
	public class AddAutoLoadAttributeAction : ActionBase
	{
		private CodeClass codeClass;

		/// <summary>
		/// Gets or sets the code class.
		/// </summary>
		/// <value>The code class.</value>
		[Input(Required = true)]
		public CodeClass CodeClass
		{
			get { return codeClass; }
			set { codeClass = value; }
		}


		private Guid uiContextGuid;

		/// <summary>
		/// Gets or sets the UI context GUID.
		/// </summary>
		/// <value>The UI context GUID.</value>
		[Input(Required = true)]
		public Guid UIContextGuid
		{
			get { return uiContextGuid; }
			set { uiContextGuid = value; }
		}

		/// <summary>
		/// Called when [execute].
		/// </summary>
		protected override void OnExecute()
		{
			string attName = "ProvideAutoLoad";
			string attValue =
				string.Format(
					CultureInfo.CurrentCulture,
					"\"{0}\"",
					this.uiContextGuid.ToString("B"));

			CodeAttribute provideAutoLoadAtt = FileCodeModelHelper.GetAttribute(this.codeClass, attName);

			if(provideAutoLoadAtt != null)
			{
				FileCodeModelHelper.UpdateAttribute(provideAutoLoadAtt, attValue);
			}
			else
			{
				FileCodeModelHelper.AddAttribute(codeClass, attName, attValue, false);
			}
		}
	}
}